package pw.hais.znyj.mod.led;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import pw.hais.sqlite.DBUtil;
import pw.hais.utils.L;
import pw.hais.znyj.R;
import pw.hais.znyj.base.app.BaseActivity;

/**
 * Created by hais1992 on 2016/11/20/020.
 */

public class LedEdieActivity extends BaseActivity implements View.OnClickListener {
    public int id;
    public static final String INTENT_PIN_KEY = "INTENT_PIN_KEY";
    private LedModel ledModel;
    private EditText edit_pin, edit_time, edit_number, edit_beizhu;
    private Button btn_push;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_led_edit);

        btn_push = (Button) findViewById(R.id.btn_push);
        edit_pin = (EditText) findViewById(R.id.edit_pin);
        edit_time = (EditText) findViewById(R.id.edit_time);
        edit_number = (EditText) findViewById(R.id.edit_number);
        edit_beizhu = (EditText) findViewById(R.id.edit_beizhu);
        btn_push.setOnClickListener(this);

        id = getIntent().getIntExtra(INTENT_PIN_KEY, -10);
        if (id != -10) {
            ledModel = (LedModel) DBUtil.findById(LedModel.class, id + "");
            if (ledModel != null) {
                getSupportActionBar().setTitle("编辑LED灯");
                btn_push.setText("修改LED灯");
                edit_pin.setText(ledModel.pin + "");
                edit_time.setText(ledModel.time + "");
                edit_number.setText(ledModel.number + "");
                edit_beizhu.setText(ledModel.remarks + "");
            } else {
                id = -10;
            }
        }

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_push:
                try {
                    if (ledModel == null) ledModel = new LedModel();
                    ledModel.pin = Integer.parseInt(edit_pin.getText() == null ? "" : edit_pin.getText().toString().trim());
                    ledModel.time = Integer.parseInt(edit_time.getText() == null ? "" : edit_time.getText().toString().trim());
                    ledModel.number = Integer.parseInt(edit_number.getText() == null ? "" : edit_number.getText().toString().trim());
                    ledModel.remarks = edit_beizhu.getText() == null ? "" : edit_beizhu.getText().toString().trim();
                    if (ledModel.pin != 0) {
                        if (id == -10) {
                            DBUtil.save(ledModel);
                            L.showShort("添加成功！");
                        } else {
                            DBUtil.update(ledModel);
                            L.showShort("修改成功！");
                        }
                        finish();
                    } else {
                        L.showShort("PIN端口号不能为0！");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    L.showShort("参数输入错误！");
                }
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_led_edit, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_delete) {
            DBUtil.deleteById(LedModel.class,this.id+"");
            L.showShort("删除成功！");
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
}
